package com.sz.app.oss.api.logistics.price;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.ModuleNames;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.core.entity.QuotationSet;
import com.sz.biz.logistics.core.service.QuotationService;
import com.sz.biz.logistics.core.dto.QuotationDto;
import com.sz.biz.logistics.core.dto.QuotationSetDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.core.service.PrincipalLogUtils;
import com.sz.app.oss.api.logistics.base.dto.ApprovalAuditMsgDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * Function: 报价方案. <br>
 * Date: 2017年03月20日 14:35 <br>
 * Author: wentao.chang
 */
@RestController
@RequestMapping("/api/v1/biz/quotations/cost")
@Api(description = " ", tags = "035-002、价格-报价方案管理-成本报价方案审核管理")
public class CostQuotationApprovalController extends BaseController {

    @Autowired
    private QuotationService quotationService;

    @ApiOperation(value = "更新报价方案的主信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quotationDto", value = "" +
                    "type(1:成本价),feeTypeId,name,bizuid,description,\r\n" +
                    "startEffectTime,endEffectTime,\r\n" +
                    "regionSchemaUid,weightSchemaUid,\r\n" +
                    "grade,settlementTypeId,cargoTypeCode,\r\n" +
                    "currencyId,\r\n" +
                    "areas:\r\n" +
                    "[{figureCode}]\r\n" +
                    "}", required = true, dataType = "ServiceDto", paramType = "body")
    })
    @RequestMapping(value = "{uid}/main/approval", method = {RequestMethod.PUT})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL})
    public ResultDto updateMain(@PathVariable String uid, @RequestBody QuotationDto quotationDto) throws ParseException {
        QuotationDto qu = quotationService.updateMain(uid, quotationDto, true);
        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_PRICE);
        dto.setData(qu);
        return dto;
    }


    @ApiOperation(value = "保存报价方案的明细信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "方案uid", dataType = "string", paramType = "path", required = true),
            @ApiImplicitParam(name = "quotationSetDto", value = "" +
                    "weightSchemaUid,feeTypeId,supplierId,serviceUid,id,quotationId," +
                    "List<FreightItem> freightItems-明细:\r\n" +
                    "FreightItem{\r\n" +
                    "toRegionUid:目的分区\r\n" +
                    "price:价格\r\n" +
                    "calcType:计价方式code\r\n" +
                    "baseValue:价格单位\r\n" +
                    "baseUnitCode：价格计量单位}\r\n" +
                    "List<FreightWeightSetting> freightWeightSettings-重量段设置:\r\n" +
                    "FreightWeightSetting{" +
                    "calcType:计价方式code\r\n" +
                    "baseValue:价格单位\r\n" +
                    "baseUnitCode：价格计量单位code}\r\n" +
                    "}", required = true, dataType = "ServiceDto", paramType = "body")
    })
    @RequestMapping(value = "/{uid}/detail/approval", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL})
    public ResultDto saveDetail(@PathVariable String uid, @RequestBody QuotationSetDto quotationSetDto) throws ParseException {
        QuotationSet set = quotationService.saveDetail(uid, quotationSetDto, true);
        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_PRICE);
        dto.setData(set);
        return dto;
    }


    @ApiOperation(value = "打回报价方案", notes = "打回报价方案-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "方案uid", dataType = "String", paramType = "path", required = true),
            @ApiImplicitParam(name = "msg", value = "消息", dataType = "ApprovalAuditMsgDto", paramType = "body", required = true)
    })
    @RequestMapping(value = "{uid}/rejections", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL})
    public ResultDto reject(@PathVariable String uid, @RequestBody ApprovalAuditMsgDto msg) throws ParseException {
        quotationService.reject(Arrays.asList(uid), msg.getMsg());
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION, UserActions.UPDATE,
                "打回报价方案：(uid)" + uid + ",消息" + msg.getMsg());
        return getRejectSuccessDto();
    }


    @ApiOperation(value = "审核报价方案", notes = "审核报价方案-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "方案uid", dataType = "String",
                    paramType = "path", required = true),
            @ApiImplicitParam(name = "msg", value = "审核意见", dataType = "ApprovalAuditMsgDto",
                    paramType = "body", required = true)
    })
    @RequestMapping(value = "{uid}/audit", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL})
    public ResultDto updateStatus(@PathVariable String uid, @RequestBody ApprovalAuditMsgDto msg) throws ParseException {
        quotationService.audit(Arrays.asList(uid), msg.getMsg());
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION, UserActions.UPDATE,
                "审核报价方案：(uid)" + uid + ",意见：" + msg.getMsg());
        return getAuditSuccessDto(MessagesCodes.MODULE_PRICE);
    }

    @ApiOperation(value = "上线报价方案", notes = "上线报价方案-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "方案uid", dataType = "String[]",
                    paramType = "body", required = true),
            @ApiImplicitParam(name = "isconfirmed", value = "是否确认，\r\n" +
                    "第一次传false,确认之后传true", dataType = "boolean",
                    paramType = "query", required = true, defaultValue = "false")
    })
    @RequestMapping(value = "/online", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL})
    public ResultDto online(@RequestBody List<String> uids,
                            @RequestParam(required = false, defaultValue = "false") boolean isconfirmed) throws ParseException {
        quotationService.online(uids, isconfirmed);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION, UserActions.UPDATE,
                "上线报价方案：(uids)" + uids);
        return getOnlineSuccessDto();
    }

    @ApiOperation(value = "下线报价方案", notes = "下线报价方案-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "方案uid", dataType = "String[]",
                    paramType = "body", required = true)
    })
    @RequestMapping(value = "/offline", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_COST_APPROVAL})
    public ResultDto offline(@RequestBody List<String> uids) throws ParseException {
        quotationService.offline(uids);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION, UserActions.UPDATE,
                "下线报价方案：(uids)" + uids);
        return getOfflineSuccessDto();
    }

}
